package client.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

public class Tile extends Canvas {
	private int value;
	Color lightForeColor = new Color(Display.getCurrent(), 249, 246, 242);
	Color darkForeColor = new Color(Display.getCurrent(), 119, 110, 101);
	
	public Tile(Composite parent, int style) {
		super(parent, style);
		
		value = 0;

		Font newfont = new Font(getDisplay(), getFont().getFontData()[0].getName(), 24, SWT.BOLD);
		setFont(newfont);
		
		addPaintListener(new PaintListener() {
			@Override
			public void paintControl(PaintEvent e) {
				FontMetrics fm = e.gc.getFontMetrics();
				int width = fm.getAverageCharWidth();
				int mx = getSize().x/2 - (""+value).length() * width/2;
				int my = getSize().y/2 - fm.getHeight()/2 - fm.getDescent();
				
				if (value > 0) {
					e.gc.drawString(""+value, mx, my);	
				}	
			}
		});
		
		addDisposeListener(disposeListener);
	}
	
	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
		changeBackgroundColor();
		redraw();
	}	
	
	private void changeBackgroundColor() {
		switch (value) {
			// Maze wall
			case -1:
				setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
				setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
				break;
			case 0: {
				Color color = new Color(Display.getCurrent(), 204, 192, 179);
				setBackground(color);
				color.dispose();
				break;
			}
			// Maze player
			case 1: {
				Color color = new Color(Display.getCurrent(), 0, 0, 255);
				setBackground(color);
				setForeground(color);
				color.dispose();
				break;
			}
			case 2:	{
				Color color = new Color(Display.getCurrent(), 238, 228, 218);
				setBackground(color);
				setForeground(darkForeColor);
				color.dispose();
				break;
			}
			// Maze goal
			case 3: {
				Color color = new Color(Display.getCurrent(), 232, 191, 40);
				setBackground(color);
				setForeground(color);
				color.dispose();
				break;
			}
			case 4:	{
				Color color = new Color(Display.getCurrent(), 237, 224, 200);
				setBackground(color);
				setForeground(darkForeColor);
				color.dispose();
				break;
			}
			case 8:	{
				Color color = new Color(Display.getCurrent(), 242, 177, 121);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 16: {
				Color color = new Color(Display.getCurrent(), 246, 149, 99);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 32: {
				Color color = new Color(Display.getCurrent(), 246, 124, 95);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 64: {
				Color color = new Color(Display.getCurrent(), 246, 94, 59);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 128: {
				Color color = new Color(Display.getCurrent(), 232, 199, 111);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 256: {
				Color color = new Color(Display.getCurrent(), 232, 200, 96);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 512: {
				Color color = new Color(Display.getCurrent(), 232, 200, 80);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 1024: {
				Color color = new Color(Display.getCurrent(), 233, 192, 56);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;
			}
			case 2048: {
				Color color = new Color(Display.getCurrent(), 232, 191, 40);
				setBackground(color);
				setForeground(lightForeColor);
				color.dispose();
				break;	
			}
			default:
				setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
				setForeground(lightForeColor);
				break;
		}
	}
	
	DisposeListener disposeListener = new DisposeListener() {
		@Override
		public void widgetDisposed(DisposeEvent e) {
			if (!isDisposed()) {
				Font font = getFont();
				if (font != null) {
					font.dispose();
					setFont(null);
				}
				
				Color background = getBackground();
				if (background != null) {
					background.dispose();
					setBackground(null);
				}
				
				Color foreground = getForeground();
				if (foreground != null) {
					foreground.dispose();
					setForeground(null);
				}
				
				if (lightForeColor != null) {
					lightForeColor.dispose();
				}
				
				if (darkForeColor != null) {
					darkForeColor.dispose();
				}
			}
		}
	};
}